package com.mjh.action;
/*
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;*/
import java.util.Date;
import java.util.HashMap;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.ActionSupport;
import com.mjh.bean.Article;
import com.mjh.bean.ArticleDetail;
import com.mjh.comm.Constant;
import com.mjh.comm.page.Pager;
import com.mjh.manager.ArticleDetailManager;
import com.mjh.manager.ArticleManager;
import com.mjh.manager.ArticleSearchManager;
import com.mjh.manager.DiscussManager;

public class ArticleAction extends ActionSupport{

	private static final long serialVersionUID = -5969814568349114819L;
	
	public static Logger log = Logger.getLogger(ArticleAction.class);
	
	private ArticleManager manager = new ArticleManager();
	
	private ArticleDetailManager detailManager = new ArticleDetailManager();
	
	private DiscussManager discussManager = new DiscussManager();
	
	private ArticleSearchManager articleSearchManager = new ArticleSearchManager();
	
	private Article article;
	
	private HashMap<String,String> rp;
	
	private Pager<Article> pager;
	
	private int result;
	
	/*	ActionContext context = ActionContext.getContext();
    Map requestm = (Map)context.get("request");
    Map sessionm = context.getSession();
    Map applicationm = context.getApplication();
	HttpServletRequest request = ServletActionContext.getRequest ();
	ActionContext ct= ActionContext.getContext();
	HttpServletRequest request2=(HttpServletRequest)ct.get(ServletActionContext.HTTP_REQUEST);
	HttpServletRequest request3 = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	HttpSession session = request.getSession();
*/	
	/**
	 * 添加文章
	 */
	public String add(){
		this.article.setPostTime(new Date());
		this.article.setClickNum(0);
		this.article.setIsIndex(Constant.ARTICLE_NOT_INDEX);
		this.result = manager.addArticle(this.article);
		return SUCCESS;
	}
	/**
	 * 修改文章
	 * @return
	 */
	public String edit(){
		result = manager.updateArticle(this.article);
		if(result > 0){
			manager.updateaArticleIndexStatus(this.article.getId());
		}
		return SUCCESS;
	}
	/**
	 * 删除文章
	 * @return
	 */
	public String del(){
		int articleId = NumberUtils.toInt(rp.get("articleId"));
		result = manager.delArticle(articleId);
		detailManager.delDetailByArticle(articleId);
		discussManager.delDiscussByArticle(articleId);
		if(result > 0){
			articleSearchManager.delIndex(articleId);
		}
		return SUCCESS;
	}
	/**
	 * 列出某个文章的所有内容，用于后台文章内容的操作
	 * @return
	 */
	public String articleDetailForAdmin(){
		int articleId = NumberUtils.toInt(rp.get("articleId"));
		this.article = manager.getArticle(articleId);
		if(this.article != null){
			this.article.setDetailsPage(detailManager.listByArticleId(articleId));
		} else {
			// return 404;
		}
		return SUCCESS;
	}
	/**
	 * 得到某个文章的单页内容，用于前台文章显示
	 * @return
	 */
	public String articleDetail(){
		int articleId = NumberUtils.toInt(rp.get("articleId"));
		int pageNo = NumberUtils.toInt(rp.get("pageNo"));
		this.article = manager.getArticle(articleId);
		if(this.article != null){
			Pager<ArticleDetail> detailsPager = detailManager.listForPage(articleId, pageNo);
			detailsPager.setPageUrl("/article/show.do?rp.pageNo=#pageNo#&rp.articleId="+rp.get("articleId"));
			this.article.setDetailsPage(detailsPager);
		} else {
			// return 404;
		}
		return SUCCESS;
	}
	/**
	 * 显示文章列表，后台
	 * @return
	 */
	public String listForAdmin(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		if(!"-1".equals(rp.get("categoryType"))){
			parameter.put("category", rp.get("categoryType"));
		}
		if(!"-1".equals(rp.get("artcletype"))){
			parameter.put("artType", rp.get("artcletype"));
		}
		if(!"-1".equals(rp.get("status"))){
			parameter.put("status", rp.get("status"));
		}
		this.pager = manager.listAll(NumberUtils.toInt(rp.get("pageNo")), parameter);
		return SUCCESS;
	}
	/**
	 * 显示文章分类列表，前台
	 * @return
	 */
	public String listCategory(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("status", Constant.ARTICLE_STATE_NORMAL);
		parameter.put("category", NumberUtils.toInt(rp.get("categoryId")));
		this.pager = manager.listAll(NumberUtils.toInt(rp.get("pageNo")), parameter);
		pager.setPageUrl("/category/list.do?rp.pageNo=#pageNo#&rp.categoryId="+rp.get("categoryId"));
		return SUCCESS;
	}
	/**
	 * 显示文章小分类列表，前台
	 * @return
	 */
	public String listArtType(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("status", Constant.ARTICLE_STATE_NORMAL);
		parameter.put("artType", NumberUtils.toInt(rp.get("artType")));
		this.pager = manager.listAll(NumberUtils.toInt(rp.get("pageNo")), parameter);
		pager.setPageUrl("/artType/list.do?rp.pageNo=#pageNo#&rp.artType="+rp.get("artType"));
		return SUCCESS;
	}
	/**
	 * 文章的点击记数
	 */
	public String click(){
		int articleId = NumberUtils.toInt(rp.get("articleId"));
		manager.clickArticle(articleId);
		return SUCCESS;
	}
	
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public Pager<Article> getPager() {
		return pager;
	}

	public void setPager(Pager<Article> pager) {
		this.pager = pager;
	}

	public HashMap<String, String> getRp() {
		return rp;
	}

	public void setRp(HashMap<String, String> requestParameter) {
		this.rp = requestParameter;
	}
}
